package com.xzm.project.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * author : xuzongmeng
 * date   : 2019/9/5
 * desc   : 动态相对布局
 */
public class DynamicRelativeLayoutActivity extends AppCompatActivity {
    private RelativeLayout rootView;
    private Button btnTest1;
    private Button btnTest2;
    private Button btnTest3;
    private Button btnTest4;
    private Button btnTest5;
    private Button btnTest6;
    private Button btnTest7;
    private Button btnTest8;
    private Button btnTest9;
    public Context mContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
//        setContentView(R.layout.activity_dynamic_relativelayout);
        setContentView(getLayoutRootView());
        initView(savedInstanceState);
        initListener();
    }
    /**
     * 视图根容器
     */
    private View getLayoutRootView() {
        rootView = new RelativeLayout(mContext);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        rootView.setLayoutParams(layoutParams);
        return rootView;
    }
    /**
     * 初始化Params
     */
    private RelativeLayout.LayoutParams getRelativeParams() {
        RelativeLayout.LayoutParams params = null;
        if (params == null) {
            params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        }
        return params;
    }



    private int idrelativeLayout1 = 0x000111;
    private int iDbtnTest1 = 0x001;
    private int iDbtnTest2 = 0x002;
    private int iDbtnTest3 = 0x003;

    private int idrelativeLayout2 = 0x000222;
    private int iDbtnTest4 = 0x004;
    private int iDbtnTest5 = 0x005;
    private int iDbtnTest6 = 0x006;


    private int idrelativeLayout3 = 0x000333;
    private int iDbtnTest7 = 0x007;
    private int iDbtnTest8 = 0x008;
    private int iDbtnTest9 = 0x009;

    protected void initView(Bundle savedInstanceState) {
        //第一个子布局容器
        RelativeLayout relativeLayout1 = new RelativeLayout(mContext);
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500);
        layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP); // 这句不写也默认 第一个子布局容器  也在第一个
        layoutParams1.setMarginStart(20);
        relativeLayout1.setPadding(0,0,50,0);
        relativeLayout1.setBackgroundColor(Color.RED);
        relativeLayout1.setId(idrelativeLayout1);
        relativeLayout1.setLayoutParams(layoutParams1);




 //       id写在xml和声明变量这两种方式等效
//        btnTest1 = new Button(mContext);
//        btnTest1.setId(R.id.btnTest1);
//        btnTest1.setText("按钮1");
//
//        btnTest2 = new Button(mContext);
//        btnTest2.setId(R.id.btnTest2);
//        btnTest2.setText("按钮2");
//
//        btnTest3 = new Button(mContext);
//        btnTest3.setId(R.id.btnTest3);
//        btnTest3.setText("按钮3");

//        relativeParam1.addRule(RelativeLayout.ALIGN_PARENT_START, R.id.btnTest1);
//        relativeParam1.addRule(RelativeLayout.CENTER_VERTICAL, R.id.btnTest1);
//
//        relativeParam2.addRule(RelativeLayout.CENTER_IN_PARENT, R.id.btnTest2);
//        relativeParam3.addRule(RelativeLayout.ALIGN_PARENT_END, R.id.btnTest3);

        btnTest1 = new Button(mContext);
        btnTest1.setId(iDbtnTest1);
        btnTest1.setText("按钮1");

        btnTest2 = new Button(mContext);
        btnTest2.setId(iDbtnTest2);
        btnTest2.setText("按钮2");

        btnTest3 = new Button(mContext);
        btnTest3.setId(iDbtnTest3);
        btnTest3.setText("按钮3");

        RelativeLayout.LayoutParams relativeParam1 = getRelativeParams();
        RelativeLayout.LayoutParams relativeParam2 = getRelativeParams();
        RelativeLayout.LayoutParams relativeParam3 = getRelativeParams();
        relativeParam1.addRule(RelativeLayout.ALIGN_PARENT_START, iDbtnTest1);
        relativeParam1.addRule(RelativeLayout.CENTER_VERTICAL, iDbtnTest1);
        relativeParam2.addRule(RelativeLayout.CENTER_IN_PARENT, iDbtnTest2);
        relativeParam3.addRule(RelativeLayout.ALIGN_PARENT_END, iDbtnTest3);
        relativeParam3.addRule(RelativeLayout.CENTER_VERTICAL, iDbtnTest3);



        btnTest1.setLayoutParams(relativeParam1);
        btnTest2.setLayoutParams(relativeParam2);
        btnTest3.setLayoutParams(relativeParam3);


        relativeLayout1.addView(btnTest1);
        relativeLayout1.addView(btnTest2);
        relativeLayout1.addView(btnTest3);

        rootView.addView(relativeLayout1);
        //第二个子布局容器
        RelativeLayout relativeLayout2 = new RelativeLayout(mContext);
        RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams2.addRule(RelativeLayout.BELOW,idrelativeLayout1);
        relativeLayout2.setBackgroundColor(Color.GREEN);
        relativeLayout2.setId(idrelativeLayout2);
        relativeLayout2.setLayoutParams(layoutParams2);


        btnTest4 = new Button(mContext);
        btnTest4.setId(iDbtnTest4);
        btnTest4.setText("按钮4");

        btnTest5 = new Button(mContext);
        btnTest5.setId(iDbtnTest5);
        btnTest5.setText("按钮5");

        btnTest6 = new Button(mContext);
        btnTest6.setId(iDbtnTest6);
        btnTest6.setText("按钮6");



        RelativeLayout.LayoutParams relativeParam4 = getRelativeParams();
        RelativeLayout.LayoutParams relativeParam5 = getRelativeParams();
        RelativeLayout.LayoutParams relativeParam6 = getRelativeParams();
        relativeParam4.addRule(RelativeLayout.ALIGN_PARENT_START, iDbtnTest4);
        relativeParam4.addRule(RelativeLayout.CENTER_VERTICAL, iDbtnTest4);
        relativeParam5.addRule(RelativeLayout.CENTER_IN_PARENT, iDbtnTest5);
        relativeParam6.addRule(RelativeLayout.ALIGN_PARENT_END, iDbtnTest6);
        relativeParam6.addRule(RelativeLayout.CENTER_VERTICAL, iDbtnTest6);

        btnTest4.setLayoutParams(relativeParam4);
        btnTest5.setLayoutParams(relativeParam5);
        btnTest6.setLayoutParams(relativeParam6);

        relativeLayout2.addView(btnTest4);
        relativeLayout2.addView(btnTest5);
        relativeLayout2.addView(btnTest6);

        rootView.addView(relativeLayout2);







        //第三个子布局容器
        RelativeLayout relativeLayout3 = new RelativeLayout(mContext);
        RelativeLayout.LayoutParams layoutParams3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                300);
        layoutParams3.addRule(RelativeLayout.BELOW,idrelativeLayout2);
        relativeLayout3.setBackgroundColor(Color.BLUE);
        relativeLayout3.setId(idrelativeLayout3);
        relativeLayout3.setLayoutParams(layoutParams3);


        btnTest7 = new Button(mContext);
        btnTest7.setId(iDbtnTest7);
        btnTest7.setText("按钮7");

        btnTest8 = new Button(mContext);
        btnTest8.setId(iDbtnTest8);
        btnTest8.setText("按钮8");

        btnTest9 = new Button(mContext);
        btnTest9.setId(iDbtnTest9);
        btnTest9.setText("按钮9");



        RelativeLayout.LayoutParams relativeParam7 = getRelativeParams();
        RelativeLayout.LayoutParams relativeParam8 = getRelativeParams();
        RelativeLayout.LayoutParams relativeParam9 = getRelativeParams();
        relativeParam7.addRule(RelativeLayout.ALIGN_PARENT_START, iDbtnTest7);
        relativeParam7.addRule(RelativeLayout.CENTER_VERTICAL, iDbtnTest7);
        relativeParam8.addRule(RelativeLayout.END_OF, iDbtnTest7);
        relativeParam8.addRule(RelativeLayout.CENTER_IN_PARENT, iDbtnTest8);
        relativeParam9.addRule(RelativeLayout.ALIGN_PARENT_END, iDbtnTest9);
        relativeParam9.addRule(RelativeLayout.CENTER_VERTICAL, iDbtnTest9);

        btnTest7.setLayoutParams(relativeParam7);
        btnTest8.setLayoutParams(relativeParam8);
        btnTest9.setLayoutParams(relativeParam9);

        relativeLayout3.addView(btnTest7);
        relativeLayout3.addView(btnTest8);
        relativeLayout3.addView(btnTest9);

        rootView.addView(relativeLayout3);

    }

    protected void initListener() {
        btnTest1.setOnClickListener(v -> Toast.makeText(mContext, "btnTest1", Toast.LENGTH_SHORT).show());
        btnTest2.setOnClickListener(v -> Toast.makeText(mContext, "btnTest2", Toast.LENGTH_SHORT).show());
        btnTest3.setOnClickListener(v -> Toast.makeText(mContext, "btnTest3", Toast.LENGTH_SHORT).show());
        btnTest4.setOnClickListener(v -> Toast.makeText(mContext, "btnTest4", Toast.LENGTH_SHORT).show());
        btnTest5.setOnClickListener(v -> Toast.makeText(mContext, "btnTest5", Toast.LENGTH_SHORT).show());
        btnTest6.setOnClickListener(v -> Toast.makeText(mContext, "btnTest6", Toast.LENGTH_SHORT).show());
    }
}
